package com.bq.gametrade.view.popwindow;

import android.app.ActionBar;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bq.gametrade.R;
import com.bq.gametrade.bean.GamesListDetails;

import java.util.List;

/**
 * Created by Feel on 2017/2/23.
 */

public class SelectGameNamesPop extends PopupWindow {

    private Activity activity;
    private List<GamesListDetails> list;
    private LinearLayout layHome;
    private ListView listView;
    private SelectGamesNameAdapter adapter;

    private String theName;

    public interface OnGetNameListener {
        void getName(int position);
    }

    public SelectGameNamesPop(Activity activity, List<GamesListDetails> list,String name) {
        super(activity);
        this.activity = activity;
        this.list = list;
        this.theName = name;
        View view = LayoutInflater.from(activity).inflate(R.layout.pop_select_gamesname, null);
        this.setWidth(ActionBar.LayoutParams.MATCH_PARENT);
        this.setHeight(ActionBar.LayoutParams.WRAP_CONTENT);
        this.setContentView(view);
//        this.setAnimationStyle(R.style.PayPasswordPopAnimation);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setOutsideTouchable(true);
        this.setBackgroundDrawable(new ColorDrawable(0x00000000));
        initView(view);

    }

    private void initView(View view) {
        listView = (ListView)view.findViewById(R.id.pop_list);
        adapter = new SelectGamesNameAdapter();
        listView.setAdapter(adapter);
    }

    public void setOnGetNameListener(final OnGetNameListener listener){
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                listener.getName(position);
                adapter.notifyDataSetChanged();
                dismiss();

            }
        });

    }


    class SelectGamesNameAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = LayoutInflater.from(activity).inflate(R.layout.item_pop_listview_selectgamename, null);
                viewHolder.tvGameName = (TextView) convertView.findViewById(R.id.tv_gameName);
                viewHolder.isSelected = (ImageView) convertView.findViewById(R.id.isSelected);


                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.tvGameName.setText(list.get(position).serverName);
            if(list.get(position).serverName.equals(theName)){
                viewHolder.isSelected.setVisibility(View.VISIBLE);
                viewHolder.tvGameName.setTextColor(Color.parseColor("#99F47C30"));
            }else {
                viewHolder.isSelected.setVisibility(View.INVISIBLE);
                viewHolder.tvGameName.setTextColor(Color.parseColor("#99000000"));
            }

            return convertView;
        }

        class ViewHolder {
            TextView tvGameName;
            ImageView isSelected;


        }
    }
}
